﻿
using RightControl.Common;
using RightControl.IService;
using RightControl.Model;
using RightControl.WebApp.Areas.Admin.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace RightControl.WebApp.Areas.DataSrv.Controllers
{
    public class AuthPlatFormController : BaseController {
        public static IProductService productService { get; set; }
        public static ISubProductImageService subProductImgService { get; set; }
        public static ISubProductSizeService subProductSizeService { get; set; }
        public static IAmazonService amazonService { get; set; }
        public static IFieldMapService fieldMapSvr { get; set; }
        public static IPlatformAuthService authService { get; set; }

        public  ActionResult Index(int? id) {
            base.Initialize(id);
            return View();
        }

        public ActionResult AmazonIndex(int? id) {
            base.Initialize(id);
            return View();
        }
        public ActionResult AmazonAuth(int? id) {
            base.Initialize(id);
            return View();
        }

        [HttpPost]
        public ActionResult AmazonAuth(PlatformAuth model) {
            Boolean isOk = false;

            //authService.GetListByFilter(model, new PageInfo(1, 10));
            if (model != null) {
                model.PlatForm = 1;
                model.UserId = Operator.UserId;
                model.CreateBy = Operator.UserId;
                model.UpdateBy = Operator.UserId;
                model.CreateOn = DateTime.Now;
                isOk = authService.InsertModel(model) > 0;
            }
            var result = isOk ? SuccessTip() : ErrorTip();
            return Json(result);
        }


        public ActionResult DhAuth(int? id) {
            base.Initialize(id);
            return View();
        }

        public ActionResult DhShop(int? id) {
            base.Initialize(id);
            return View();
        }

        public ActionResult DhAuthResult() {
            //http://localhost:27948//datasrv/AuthPlatForm/DhAuth#access_token=hb3VpFcGPKS8fSBFOZzgqLzF96nHnHwXA3yizMoT&expires_in=1586352000534&state=null
            return View();
        }

        /// <summary>
        /// 敦煌授权回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ActionResult AuthFromDH(string access_token, string expires_in, string state) {
            Boolean isOk = false;
            if (expires_in.IsNotEmpty() && access_token.IsNotEmpty()) {
              
                var model = new PlatformAuth(Operator.UserId, (int)PlatFormEnum.DhGate);                
                model.AuthToken = access_token;
                model.ExpireTime = expires_in.GetTime();

                if (state.IsNotEmpty() && state.StartsWith("SDL_")) {
                    string id = state.Replace("SDL_", "");
                    if (StringPlus.IsNumberId(id)) {
                        model.Id = id.ToInt();
                        model.UserId = Operator.UserId;
                        isOk = authService.UpdateModel(model, "ExpireTime,AuthToken");
                    }
                }
                else {
                    model.ShopName = state;
                    model.CreateOn = DateTime.Now;
                    isOk = authService.InsertModel(model) > 0;
                }
            }
            var result = isOk ? SuccessTip() : ErrorTip();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult List(PageInfo pageInfo, PlatformAuth param) {

            if (param != null) {
                param.UserId = Operator.UserId;
                dynamic result = authService.GetListByFilter(param, new PageInfo(1, 10));
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult Delete(int id,int platform) {
            PlatformAuth auth = new PlatformAuth(Operator.UserId, platform);
            auth.Id = id;
            var result = authService.Delete(auth) ? SuccessTip() : ErrorTip();
            return Json(result);
        }
    }
}